/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ua.i.kotionokyurievna.jewelryweb.controllers;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import ua.i.kotionokyurievna.jewelryweb.entities.User;
import ua.i.kotionokyurievna.jewelryweb.facade.UserFacade;
import ua.i.kotionokyurievna.jewelryweb.util.PasswordEncryptor;

/**
 *
 * @author kotionokyurievna
 */
@ManagedBean
@ViewScoped
public class LoginPageController {

    private String login;
    private String password;
    @EJB
    private UserFacade userFacade;
    PasswordEncryptor encryptor;

    public LoginPageController() {
        this.encryptor = new PasswordEncryptor();
    }

    public LoginPageController(String login, String password) {
        this.login = login;
        this.password = password;
        this.encryptor = new PasswordEncryptor();
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    
    public String loginUser(){
        User user = userFacade.findUserByLogin(login);
        if (user == null){
            return "errorPage";
        }
        else{
            String ePassword = encryptor.encrypt(password);
            if ( user.isLegalUser(ePassword)){
                return "shopping";
            }
        }
        return "errorPage";
        
    }
    
    public String register(){
        return "registrationPage";
    }
    
}
